<script>
import { mapState } from 'vuex'

export default {
  computed: {
    ...mapState(['userInfo'])
  },
  onLaunch() {
    // 检查登录状态
	console.log("检查登录状态")
    //this.checkLogin()
	
	this.checkLoginStatus()
  },
  onShow(){
	  this.checkLoginStatus()
  },
  methods: {
    /* async checkLogin() {
      try {
        const token = uni.getStorageSync('token')
        if (token) {
          // 获取用户信息
          await this.$store.dispatch('getUserInfo')
        } else {
          // 跳转到登录页
          uni.redirectTo({
            url: '/pages/login/index'
          })
        }
      } catch (e) {
        uni.redirectTo({
          url: '/pages/login/index'
        })
      }
    } */
	checkLoginStatus() {
	      const currentPage = getCurrentPages().pop()?.route || ''
	      const publicPages = ['pages/login/index', 'pages/login/register']
	      const isPublicPage = publicPages.some(path => currentPage.includes(path))
	      
	      if (!this.$store.state.token && !isPublicPage) {
	        uni.redirectTo({
	          url: '/pages/login/index'
	        })
	      }
	    }
	
  },
  watch: {
    userInfo(newVal) {
      if (!newVal) {
        uni.redirectTo({
          url: '/pages/login/index'
        })
      }
    }
  }
}
</script>

<style>
page {
  height: 100%;
  /* overflow: hidden;  */ 
}
view, scroll-view {
  box-sizing: border-box;
}
</style>